#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;
const ll N = 11;
ll s[N], b[N];
ll n;
ll ans = 1e18;
void dfs(ll id, ll s_s, ll s_b)
{
    if (id > n)
    {
        if (s_s == 1 && s_b == 0)
            return;
        ans = min(ans, abs(s_s - s_b));
        return;
    }
    dfs(id + 1, s_s * s[id], s_b + b[id]);
    dfs(id + 1, s_s, s_b);
}
void solve()
{
    cin >> n;
    for (ll i = 1; i <= n; i++)
    {
        cin >> s[i] >> b[i];
    }
    dfs(1, 1, 0);
    cout << ans;
}
int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
